home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / COMM.LIB < prev    next >
Text File  |  1993-11-14  |  3KB  |  82 lines

  1. // Comm.lib - Serial communications library
  2. //
  3. //
  4. //**** CommOpen(): Open Comm port
  5. // SYNTAX: int CommOpen(string CommPortName,int Error)
  6. // WHERE: CommPortName: port name, such as "COM1" or "COM2"
  7. //        Error: Set errorcode if returning 0
  8. // RETURN: Handle for this comm port.  NULL if could not open
  9. // NOTES: You should be sure to close com port when finished
  10. //
  11. //
  12. //**** CommClose(): Close Comm port
  13. // SYNTAX: void CommClose(int CommHandle)
  14. // WHERE: CommHandle: value returned from previous call to CommOpen
  15. //
  16. //
  17. //**** CommWrite(): Write to comm port
  18. // SYNTAX: int CommWrite(int CommHandle,byte[] BufferArea,int BufferLength,int BytesWritten)
  19. // WHERE: CommHandle: value returned from previous call to CommOpen
  20. //        BufferArea: data bytes to write
  21. //        BufferLength: How many bytes to write
  22. //        BytesWritten: Set to how many bytes were written
  23. // RETURN: 0 for success, else error code
  24. //
  25. //
  26. //**** CommRead(): Read from comm port
  27. // SYNTAX int CommRead(int CommHandle,byte[] BufferArea,int MaxBufferLength,int BytesRead)
  28. // WHERE: CommHandle: value returned from previous call to CommOpen
  29. //        BufferArea: data bytes to read to (created if necessary)
  30. //        MaxBufferLength: Maximum bytes to read
  31. //        BytesRead: Set to how many bytes were read
  32. // RETURN: 0 for success, else error code
  33. //
  34. //
  35.  
  36. assert( defined(_FILEIO_LIB) );
  37. assert( defined(_DEVIOCTL_LIB) );
  38. #define _COMM_LIB 1
  39.  
  40. /*****************************************************************
  41.  *********        END OF DESCRIPTION FOR COMM.LIB        *********
  42.  *****************************************************************/
  43.  
  44. #define  COMM_IOCTL_CATEGORY  1
  45.  
  46. CommOpen(CommPortName,Error)
  47. {
  48.    Error = DosOpen(CommPortName,_RetCommFileHandle,_ActionTaken,0,FILE_NORMAL,
  49.                    OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  50.                    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE,NULL );
  51.    if ( Error )
  52.       _RetCommFileHandle = 0;
  53.    return _RetCommFileHandle;
  54. }
  55.  
  56. CommClose(CommHandle)
  57. {
  58.    DosClose(CommHandle);
  59. }
  60.  
  61. CommRead(CommHandle,BufferArea,BufferLength,BytesRead)
  62. {
  63.    return DosRead(CommHandle,BufferArea,
  64.                   min(QueryCharsInReceiveQueue(CommHandle),BufferLength),
  65.                   BytesRead);
  66. }
  67.  
  68. CommWrite(FileHandle,BufferArea,BufferLength,BytesWritten)
  69. {
  70.    return DosWrite(FileHandle,BufferArea,BufferLength,BytesWritten);
  71. }
  72.  
  73. QueryCharsInReceiveQueue(CommHandle)
  74. {
  75.    #define QUERY_RCV_FUNCTION 0x68
  76.    _retSize = 0;
  77.    _rc = DosDevIOCTL(CommHandle,COMM_IOCTL_CATEGORY,QUERY_RCV_FUNCTION,
  78.                      NULL,0,0,_data,4,_retSize);
  79.    return ( 0 == _rc  &&  4 == _retSize ) ? BLObGet(_data,0,UWORD16) : 0 ;
  80. }
  81.  
  82.